import { getHitokoto } from '@/apis'

/*
 * @Description
 * @Author hanl
 * @Date 2023-03-03 11:47:58
 * @LastEditors hanl
 * @LastEditTime 2024-07-19 17:59:02
 */
// https://v1.jinrishici.com/all.json
import { useRequest } from 'alova/client'

/** 获取诗词 */
export async function useHitokoto() {
  const { data, send } = useRequest(
    getHitokoto,
    {
      initialData: {
        hitokoto: '',
        creator: '',
        from: '',
        type: ''
      },
      immediate: false
    }
  )

  await send()

  const hitokothContent = computed(() => data.value.hitokoto)
  const hitokothAuthor = computed(() => data.value.from_who)
  const hitokothCategory = computed(() => cEnumMap[data.value.type])
  const hitokothOrigin = computed(() => data.value.from)

  const intervalSend = (/** 请求时间 */ time = 1000) => {
    return useIntervalFn(() => {
      send()
    }, time)
  }

  const timeoutSend = (/** 请求时间 */ time = 1000) => {
    return useTimeoutFn(() => {
      send()
    }, time)
  }

  return {
    /** 名言 */
    content: hitokothContent,

    /** 作者 */
    author: hitokothAuthor,

    /** 类别 */
    category: hitokothCategory,

    /** 来源 */
    origin: hitokothOrigin,

    /** 发送请求 */
    send,

    /** 间断性发起请求 */
    intervalSend,

    /** N秒后发起请求 */
    timeoutSend
  }
}
